package com.example.demo.config;

import cn.creditease.std.consumer.JobConsumer;
import cn.creditease.std.consumer.JobConsumerRegistory;
import cn.creditease.std.model.Job;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
import java.util.List;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: kai.yang
 * @Date: 2019-08-13 16:47
 */
@Component
public class MqTest {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    ConnectionFactory connectionFactory;


    /**
     * mq交互机命名
     */
    private static final String ALLOT_MQ_CHANNEL_EXCHANGE = "testset";


    /**
     * mq消息队列命名
     */
    private static final String ALLOT_MQ_CHANNEL_ROUNTING = "testset";

    /**
     * 默认队列命名
     */
    private static final String ALLOT_MQ_CHANNEL_QUEUE = "testset";

    /**
     * 默认队列命名
     */
    private static final String ALLOT_MQ_CHANNEL_QUEU_2 = "testset";

    public MqTest(RabbitTemplate rabbitTemplate,
        ConnectionFactory connectionFactory) throws IOException {
        this.rabbitTemplate = rabbitTemplate;
        this.connectionFactory = connectionFactory;
        Connection connection = connectionFactory.createConnection();
        // TODO: 2019-08-07 重连机制验证
        com.rabbitmq.client.Channel channel = connection.createChannel(true);

        channel.exchangeDeclare(ALLOT_MQ_CHANNEL_EXCHANGE, BuiltinExchangeType.FANOUT, true, false, null);

        // 声明队列
        channel.queueDeclare(ALLOT_MQ_CHANNEL_QUEUE, true, false, true, null);

        // 声明队列
        channel.queueDeclare(ALLOT_MQ_CHANNEL_QUEU_2, true, false, true, null);

        // 绑定队列和交换机
        channel.queueBind(ALLOT_MQ_CHANNEL_QUEUE, ALLOT_MQ_CHANNEL_EXCHANGE, ALLOT_MQ_CHANNEL_ROUNTING);


        // 绑定队列和交换机
        channel.queueBind(ALLOT_MQ_CHANNEL_QUEU_2, ALLOT_MQ_CHANNEL_EXCHANGE, ALLOT_MQ_CHANNEL_ROUNTING);

        //消费者，开始接收消息
        channel.basicConsume(ALLOT_MQ_CHANNEL_QUEUE, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) {
                try {
                    System.out.println("*************队列一消费*****************************");

                } catch (Throwable e) {
                    //do nothing
                    e.printStackTrace();
                } finally {
                }
            }
        });

        //消费者，开始接收消息
        channel.basicConsume(ALLOT_MQ_CHANNEL_QUEU_2, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) {
                try {
                    System.out.println("*************队列二消费*****************************");

                } catch (Throwable e) {
                    //do nothing
                    e.printStackTrace();
                } finally {
                }
            }
        });
    }


    public void send() {
        rabbitTemplate
            .convertAndSend(ALLOT_MQ_CHANNEL_EXCHANGE, ALLOT_MQ_CHANNEL_ROUNTING,
                "sdfsdfsdf");
    }




}
